home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Communication / BookMark Manager 1.521 / JonsCommands / Jon’s Commands Read Me < prev   
Text File  |  1995-09-24  |  24KB  |  415 lines

  1. Jon╒s Commands
  2. Copyright 1993-95 by Jon Pugh
  3. (408) 974-0629
  4. jonpugh@netcom.com
  5.  
  6. You can always find the latest version on my web page: http://www.infoworkshop.com/~jonpugh/
  7.  
  8. Free for noncommercial use.
  9. Contact me for a simple cheap license if you wish to include any or all of these as part of a software solution that you sell.
  10.  
  11. This AppleScript scripting addition provides AppleScript and all scriptable and Apple Event aware applications with added functionality.  To examine the AppleScript syntax of each of these commands, simply drag Jon╒s Commands onto the Script Editor or use the Open Dictionary command to open it.  To install it, simply drop it into the Scripting Additions folder which is inside your Extensions folder.
  12.  
  13. Jon╒s Commands includes these commands:
  14.    deleteFile - delete a file, empty folder or a list of files and/or empty folders
  15.    renameFile - rename a file or folder
  16.    moveFile - move a file, folder or a list of files and/or folders to another folder
  17.    copyFile - copy a file, folder or a list of files and/or folders
  18.    sound volume - get the volume setting
  19.    set sound volume to - set the volume setting
  20.    clipboard info - get a list of data on the clipboard
  21.    set the clipboard to - put data on the clipboard
  22.    the clipboard - get data from the clipboard
  23.    execute FKEY - run an FKEY resource
  24.    screen list - describe the monitor configuration
  25.    finder selection - return the Finder╒s selection
  26.    keys pressed - get a list of pressed keys
  27.    machine environment - get info about the machine via Gestalt
  28.    play sound - play sound resources, files and descriptors
  29.    run script resource - run an ╘Scpt╒ or ╘scpt╒ script resource
  30.    free memory - return the free memory available
  31.    the ticks - return the current value of tickCount
  32.    walk folders - walk folders and run a script on each file
  33.    set cursor to - provide feedback with the cursor
  34.    AE user interaction level - control the interaction level
  35.    choose color - use the color picker to select a color
  36.  
  37. Jon╒s Commands includes these objects:
  38.    picture - for getting them off the clipboard ╟class PICT╚
  39.    sound - for getting them off the clipboard & playing them ╟class snd ╚
  40.    screen info - for describing the monitors
  41.    environment info - for describing the machine
  42.  
  43. Jon╒s Commands includes these coercions:
  44.    string to file specification - ╘TEXT╒ to ╘fss ╒
  45.    styled text to file specification - ╘STXT╒ to ╘fss ╒
  46.    international text to file specification - ╘itxt╒ to ╘fss ╒
  47.  
  48. Notes for specific commands:
  49.  
  50. finder selection:
  51.  
  52. This Addition gets the Finder╒s selected files and folders. This works with all 7.x Finders (up to and including the scriptable Finder). Using it with any newer Finder could cause it to fail.  Hopefully it won╒t crash, but it probably won╒t work.  I don╒t check for the version because it hasn╒t broken yet and people were annoyed by the limitation.
  53.  
  54. You can place script applications in the Apple Menu Items folder or with either OSA Menu or OtherMenu╒s script menu and use the finder selection function to have your script operate on the selected files.  Way cool.
  55.  
  56. Compared to the Scriptable Finder, which returns a list of object specifiers, this returns a list of aliases (or just one).
  57.  
  58. example:
  59. finder selection
  60.  
  61. copyFile, deleteFile, moveFile & renameFile:
  62.  
  63. These commands work on files and lists of files.  You can use file references (file "disk:folder:foo"), strings of pathnames or aliases to specify the files and folders for these commands. In contrast to the Scriptable Finder, these functions don╒t put up dialogs, batch their operations and use temporary memory when needed.
  64.  
  65. RenameFile can only change the name of a file or folder.  The source is a file or folder reference, alias or pathname and the destination is just the new name.  It cannot be a new location.
  66.  
  67. DeleteFile is permanent and immediate.  Be very careful with it.  I recommend using ╥moveFile foo to (path to trash)╙ if you are at all worried or inclined toward cautiousness. This can delete empty folders.
  68.  
  69. MoveFile only moves files or folders around on the disk they are on.  It does NOT copy between disks.  It cannot rename or replace files either.  The destination is a folder, which you can specify either with a file reference, an alias or as a pathname string.
  70.  
  71. CopyFile uses temporary memory if available and tries to copy the file in 1 pass for maximum speed. The source may be either a file or a folder, or list of files and/or folders. The destination may be either a file or folder to copy to, unless the source is a folder in which case the destination must be a folder. The source folder will be copied into the destination folder. You cannot replace an existing folder so the replacing parameter is ignored when copying folders. You cannot rename a folder while copying it, so you╒ll need to use renameFile after the copy completes.
  72.  
  73. If you try to replace a busy file, the copy will succeed but the busy file will be left in the temporary items folder and will appear in the Rescued Items folder in the trash after the next boot. It will then be safe to delete when you next empty the trash.
  74.  
  75. example:
  76. copyFile "disk:folder:foo" to "disk2:folder:bar" with replacing
  77. copyFile "disk:folder:foo" to "disk2:folder:bar" replacing yes
  78. copyFile "disk:folder:foo" to "disk2:folder:bar" without replacing
  79. copyFile "disk:folder:foo" to "disk2:folder:bar" replacing ask
  80. deleteFile "disk:folder:foo"
  81. deleteFile file "disk:folder:"
  82. moveFile alias "disk:folder:foo" to "disk:folder2:"
  83. renameFile alias "disk:folder:foo" to "bar"
  84. copyFile {"disk:folder:foo", alias "disk2:folder:bar:"} to "disk3:folder:"
  85. deleteFile {file "disk:folder:foo", "disk2:folder:bar"}
  86.  
  87. execute FKEY:
  88.  
  89. This loads the FKEY resource and jumps to it. It does not press command-shift-N, so it will not trigger QuicKeys macros or any function which patches things to run at FKEY-like key combinations.
  90.  
  91. example:
  92. execute FKEY "Switch-a-roo"
  93. execute FKEY 5
  94.  
  95. keys pressed:
  96.  
  97. Keys Pressed returns the GetKey info in the form of a list of key names in string form.
  98.  
  99. example:
  100. keys pressed contains "option"
  101. keys pressed = {"option","command"}
  102.  
  103. screen list:
  104.  
  105. Screen List returns a list of screen info records which describe the monitors attached to the computer. You can specify which screen is first in the returned list by using the ╥starting with╙ parameter which takes either ╥main screen╙, ╥deepest screen╙ or ╥largest screen╙. The main screen is the one with the menu bar on it.  It will always have 0,0 as its upper left coordinate. The deepest screen is the one with the most colors and closest to the main screen if there are more than one. The largest screen is the one with the most pixels. If no parameter is specified, the main screen is returned as the first item in the list.
  106.  
  107. The screen info record has these properties:
  108.  
  109. screen size point  -- The height and width of the screen.
  110. bounds bounding rectangle  -- The boundary rectangle for the screen.
  111. color depth small integer  -- The screen color depth setting.
  112. in color boolean  -- If the screen is in color (versus grayscale).
  113.  
  114. example:
  115. screen list
  116. number of items of (screen list)
  117. bounds of item 1 of (screen list starting with largest screen)
  118.  
  119. machine information:
  120.  
  121. Machine Information returns a keyworded record object of common machine information from Gestalt. You can also feed it a Gestalt selector and it will return the long word response.  This provides complete access to all of the Gestalt functions that exist. You can use the ╥check bit╙ parameter to return a boolean which tells if that bit is on.  Bits are numbered from 1 to 32 with number 1 being the lowest or rightmost bit. In this way you can get any information from Gestalt.
  122.  
  123. The environment info record provides these properties:
  124.  
  125. machine type string  -- The name of the Macintosh model.
  126. CPU type string  -- The CPU installed.
  127. System version string  -- The current System version.
  128. FPU boolean  -- Does this machine have an FPU?
  129. Color Quickdraw boolean  -- Does this machine have color Quickdraw?
  130. AppleTalk version extended real  -- The current AppleTalk version.
  131. keyboard string  -- The name of the main keyboard.
  132. Data Access Manager boolean  -- Does this machine have the Data Access Manager installed?
  133. software power off boolean  -- Does this machine turn off when shutdown?
  134. logical RAM integer  -- Amount of RAM available to the system.
  135. physical RAM integer  -- Amount of RAM installed in this machine.
  136. active scripts integer  -- Number of script systems active.
  137. virtual memory boolean  -- Is virtual memory on?
  138. scriptable Finder boolean  -- Is the scriptable Finder present?
  139. owner name string   -- The owner name in sharing setup.
  140. sharing name string  -- The machine name in sharing setup.
  141.  
  142. Both this and keys pressed use STR# resources if you are inclined to localize them.  The machine info is a subset of the available Gestalt selectors.  If you have need of others, let me know and we might be able to include them in a future version, although you can get raw information for any gestalt selector directly.
  143.  
  144. example:
  145. machine information
  146. scriptable Finder of machine information
  147. machine information "code"
  148. machine information "code" checking bit 0
  149.  
  150. clipboard info, the clipboard & set the clipboard to:
  151.  
  152. All of these can manage multiple data types.  Styled text is the default data type, with plain text (known as string) as the backup.  International text is also supported, as are a lot of other data formats.  Almost any resource type can be manipulated in AppleScript as an AEDesc in applications. Pictures and sounds are also popular to move around.
  153.  
  154. There is a problem with using the clipboard in multiple programs.  Thus, you can only use these commands in the front application. The simplest thing to do is use ╥activate╙ first to get the application to the front and then nab or grab the clipboard.  Some applications don╒t seem to check if the scrap has changed while they are running since it only changes when you switch layers in System 7.  In this case you can simply activate an app and then activate the original.  
  155.  
  156. example:
  157. clipboard info
  158. set foo to the clipboard
  159. set foo to the clipboard as "PICT"
  160. set the clipboard to foo
  161.  
  162. sound volume & set sound volume to:
  163.  
  164. Both of these use a number between 0 and 7 for the volume, just like everyone else.
  165.  
  166. example:
  167. set oldVol to sound volume
  168. set sound volume to 5
  169. set sound volume to oldVol
  170.  
  171. play sound:
  172.  
  173. This can play sound (╘snd ╒) resources installed in the system or the current open files by either name or id number. It can also play sound resources out of files and out of AppleScript variables (typically accessed via the clipboard). You cannot interrupt a sound in progress in this version and the sounds are played synchronously.  This command is a small improvement on Donald╒s sample code from the Language Reference Manual.
  174.  
  175. example:
  176. play sound "I╒m sorry Dave..."
  177.  
  178. run script resource:
  179.  
  180. This matches the AppleScript Run Script command but works on resources instead of files.  This allows you to bundle several scripts (typically from other OSA components such as QuicKeys) into a single script file or application. I╒ve used it to place the ╘scpt╒ resources from QuicKeys scripts into an applet and run them from there.
  181.  
  182. There is a problem with having more than 1 ╘scpt╒ resource per file and that is the Script Editor.  It finds the script to display by using Get1IndResource('scpt', 1) which can return the wrong resource if you have several. If you are literate with ResEdit you can make sure the proper script resource is last in the file, but that╒s less certain than the other technique I recommend, which is to change the script resource╒s type to ╘Scpt╒ which is not recognized by the Script Editor.  You need to copy the script resource as hex data and paste it into a new empty ╘Scpt╒ resource.  This way allows no conflicts.  Another solution is to use my ScriptServer application (available on Info-Mac & gaea) which allows you to create script resources of any type and id.  This makes it simple to create a script application which combines AppleScript and QuicKeys scripts.  You can even record a QuicKeys script, decompile it with ScriptServer, edit it and recompile it as a piece of a script application.
  183.  
  184. Yet another option is simply to record a QuicKeys script, copy it and paste the text into your AppleScript script with the ╥run script foo in QuicKeys╙ command with foo as your text or a variable with text in it.  This works well with simple scripts and doesn╒t require this osax.  Unfortunately, the Run Script 1.1 osax has a memory leak when doing this.  Running a compiled script resource is marginally faster too.
  185.  
  186. You can reference script resources by name or number.
  187.  
  188. example:
  189. run script resource "Flail madly"
  190.  
  191. free memory:
  192.  
  193. This returns the current value of the function FreeMem.  It shows the amount of free memory available in the current application╒s current heap.  Useful for finding memory leaks. You can also specify which heap you would like info about with the constants appZone and sysZone.
  194.  
  195. example:
  196. free memory
  197. free memory sysZone
  198.  
  199. the ticks:
  200.  
  201. This returns the current value of TickCount.  This is useful for timing functions from scripts.  Unfortunately, this opens the resource file for Jon╒s Commands every time and thus affects your timing slightly, making it more useful for large timings where the overhead is less instead of small ones. The example is a script object which can record a series of times using the ticks. Include it at the beginning of your script and call it╒s methods.
  202.  
  203. example:
  204. script SmartTimer
  205.     property IMrunning : false
  206.     property startTime : 0
  207.     property endTime : 0
  208.     property timeLog : {}
  209.     
  210.     on startTimer()
  211.         if IMrunning then
  212.             error "Timer already running"
  213.         end if
  214.         try
  215.             set startTime to the ticks
  216.             set IMrunning to true
  217.         on error theMsg number -1708
  218.             error "You need Jon╒s Commands installed to use this script object."
  219.         end try
  220.     end startTimer
  221.     
  222.     on stopTimer()
  223.         if not IMrunning then
  224.             error "Timer is not running"
  225.         end if
  226.         set endTime to the ticks
  227.         set IMrunning to false
  228.         set elapsedTime to endTime - startTime
  229.         set timeLog to timeLog & {elapsedTime}
  230.         return elapsedTime
  231.     end stopTimer
  232.     
  233.     on averageTime()
  234.         set sum to 0
  235.         set n to number of items of timeLog
  236.         repeat with i in timeLog
  237.             set sum to sum + i
  238.         end repeat
  239.         return sum / n
  240.     end averageTime
  241.     
  242.     on getLog()
  243.         return timeLog
  244.     end getLog
  245.     
  246.     on clearLog()
  247.         set timeLog to {}
  248.     end clearLog
  249. end script
  250.  
  251. walk folders:
  252.  
  253. This makes file processors very simple and fast.  It takes a list of files and folders and a script. It then walks through them all and sends each file to an open handler in the script. Note that as of version 1.3.4, walk folders steps backwards through directories (reverse alphabetical order) so that you can delete files without affecting the subsequent indexing. It is possible to make this be an option if people whine enough.
  254.  
  255. The ╥only using files of type╙ parameter allows you to specify one or more file types that will be operated on.  Only files of the types specified will be sent to your script parameter. The types are 4 character strings with the obscure codes you will have to find out about.  Normal choices are things like "APPL", "osax" and "TEXT".
  256.  
  257. In addition, there are 2 boolean parameters, ╥using files╙ and ╥using folders╙.  ╥using files╙ defaults to true and causes all files to be run through the script.  ╥using folders╙ defaults to false and causes all folders to be run through the script.  You can set them to true or false by using ╥with using folders╙ and/or ╥without using files╙. 
  258.  
  259. example:
  260. on open (theFiles)
  261.     script foo
  262.         on open (theFile)
  263.             display dialog theFile as string
  264.         end open
  265.     end script
  266.     walk folders theFiles with script foo only using files of type {"TEXT", "osas"}
  267. end open
  268.  
  269. set cursor to:
  270.  
  271. This allows you to change the current cursor to indicate a busy script. It is problematic since the front application can change it right back, and many do, but it can be very useful nonetheless.
  272.  
  273. You can use the five standard cursors; arrow cursor, watch cursor, I beam cursor, cross cursor & plus cursor, plus a busy cursor which spins.  You can also specify the name or number of a CURS resource or, using the ╥with cursor list╙ parameter, an acur resource.  Subsequent calls with the ╥busy cursor╙ will increment this cursor one step.
  274.  
  275. The busy cursor keeps an acur and all the CURS resources in memory between calls.  The ╥with cursor list╙ parameter is only necessary on the first call and will reinitialize the cursor if always supplied (i.e. the cursor will not spin).  You can clear this memory by setting the cursor to something other than a busy cursor (i.e. watch cursor or arrow cursor).
  276.  
  277. These are the busy cursors available in Jon╒s Commands, use their names or numbers:
  278.  
  279.  
  280. ╩╩
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300. This does not support color cursors yet.
  301.  
  302. example:
  303. set cursor to busy cursor with cursor list "Zen Cursor"
  304. repeat
  305.     set cursor to busy cursor
  306. end repeat
  307. set cursor to watch cursor
  308.  
  309. AE user interaction level:
  310.  
  311. This allows you to get and set the AppleEvent manager╒s user interaction level.  This way you can allow interaction from remote machines or disallow interaction from local scripts.  In order for this to work, the program which is doing the interaction must explicitly check if interaction is allowed.  Hopefully all scriptable applications do.
  312.  
  313. This returns the current state and then optionally sets it to the new state.
  314.  
  315. The only drawback with this command is that you cannot turn off user interaction in the current application from a script run by OSA Menu.  Since the lowest level is ╥AE interact with self╙ and a script running by OSA Menu is sending events to the same application, this is always allowed so they think thy can put up dialogs.  Applications need to support a ╥never interact╙ mode explicitly.  Apple╒s PhotoFlash does. (shameless plug from one of the authors)
  316.  
  317. example:
  318. set oldLevel to AE user interaction level AE interact with self
  319. AE user interaction level oldLevel
  320.  
  321. choose color:
  322.  
  323. This function displays the standard color picker and returns an RGB color value.  You can specify the prompt and the initial color. RGB colors can be specified via a list of three numbers between 0 and 65535.  It╒s actually a 3 word data value which can be coerced to and from a list.
  324.  
  325. example:
  326. choose color
  327. set newColor to choose color "Pick your favorite color:" starting color {0,0,0}
  328.  
  329. example script:
  330. -- paste an HTML color reference into BBEdit
  331. property lastColor : {35000, 35000, 35000}
  332. set lastColor to choose color "Choose a background color:" starting color lastColor
  333. set s to "#"
  334. set a to "0123456789ABCDEF"
  335. repeat with i in lastColor
  336.     set q to round (i / 65535 * 256)
  337.     set h to q div 16
  338.     set l to q mod 16
  339.     set s to s & character (h + 1) of a & character (l + 1) of a
  340. end repeat
  341. tell application "BBEdit 3.5"
  342.     set selected text of window 1 to s
  343. end tell
  344.  
  345. Coercions:
  346.    string to file specification
  347.    styled text to file specification
  348.    international text to file specification
  349.  
  350. These coercions allow you to use strings where a program or osax expects a file specification.  Note that the AppleScript class ╥file╙ is really an object reference, not simply an FSSpec, although it can be coerced to one.  You can create an FSSpec by using this command: ╥set x to "disk:folder:file" as ╟class fss ╚╙. They are particularly easy to implement. With these coercions you don╒t need to worry about them though.
  351.  
  352.  
  353. Version History:
  354.  
  355. 1.0b1 - first public release
  356. 1.0b2 - fixed key name in ╥keys pressed╙ for PowerBook keyboard.
  357. 1.0b3 - added 7.1.1 to version check for ╥finder selection╙.
  358. 1.0b4 - added ╥run script resource╙ command.
  359. 1.0b5 - added ╥free memory╙ and ╥the ticks╙ commands.
  360. 1.0 - official release.
  361. 1.1b1 - added ╥copyFile╙ command. Made ╥moveFile╙, ╥deleteFile╙, ╥renameFile╙ & ╥copyFile╙ accept lists of files, aliases or pathnames. Added string to file specification coercion.
  362. 1.1b2 - fixed bug in ╥set clipboard to╙ which could cause changes to the clipboard to be unnoticed and fixed a really minor bug in ╥execute FKEY╙ which passed a dirty PC to the FKEY in 24 bit mode.
  363. 1.1b3 - added ╥walk folders╙ command, made ╥finder selection╙ return aliases for AS 1.0 compatibility and made ╥screen list╙ work without color QuickDraw.
  364. 1.1 - official release
  365. 1.1.1b1 - fixed ╥machine environment╙ property name conflict with ╥machine╙ by renaming it ╥machine type╙ and actually implemented the ╥cpu type╙ property. Also fixed the sample script for ╥walk folders╙ above.
  366. 1.1.1b2 - fixed ╥execute fkey╙ error handling, twiddled clipboard code to remove debugger calls & clean up the code a bit, fixed ╥finder selection╙ when items were on desktop of volumes other than the startup volume & made ╥walk folders╙ send an alias instead of a file specification for compatibility with AS 1.0.
  367. 1.1.1b3 - fixed error handler in ╥play sound╙.
  368. 1.1.1 - official release
  369. 1.1.2b1 - made string to fsspec coercion more picky so that it doesn╒t confuse the ╥run script╙ osax.
  370. 1.1.2 - official release
  371. 1.1.3b1 - recompiled with debugger breaks off. Ooops.
  372. 1.1.3 - official release
  373. 1.2b1 - added ╥set cursor to╙ command.
  374. 1.2b2 - added ╥AE user interaction level╙ function.
  375. 1.2 - official release
  376. 1.3b1 - added ╥only using files of type╙ parameter to ╥walk folders╙ command.
  377. 1.3 - official release
  378. 1.3.1b1 - made aete unpurgeable to avoid Script Editor dictionary bug.
  379. 1.3.1 - official release
  380. 1.3.2b1 - added ╥scriptable Finder╙ to ╥machine environment╙ record.
  381. 1.3.2b2 - added additional acur and CURS resources and spruced up the readme.
  382. 1.3.2b3 - made ╥copyFile╙ delete the temp file even if FSpExchangeFile fails.  Also fixed memory leak in ╥keys pressed╙.
  383. 1.3.2b4 - honest, this time ╥copyFile╙ is fixed.  It was FSpDelete that was failing.
  384. 1.3.2b5 - replaced guts of ╥copyFile╙ with MoreFiles code. Now it works with drop boxes. Spruced up the readme some more.
  385. 1.3.2 - official release
  386. 1.3.3b1 - Made clipboard commands error when in the background.
  387. 1.3.3b2 - Fixed ╥copyFile╙ problem when replacing a nonexistent file.
  388. 1.3.3 - official release
  389. 1.3.4b1 - Made ╥walk folders╙ command step backwards through folders.
  390. 1.3.4b2 - Made ╥copyFile╙ fail when copying to a nonexistant directory.
  391. 1.3.4b3 - Made ╥copyFile╙ return better errors when files and directories are missing. Zeroed location when not replacing so that the Finder will put the icon in the first empty spot.
  392. 1.3.4b4 - Made ╥copyFile╙ copy folders too.
  393. 1.3.4 - official release
  394. 1.3.5b1 - Fixed ╥copyFile╙ bug when copying file to file with a different name.
  395. 1.3.5 - official release
  396. 1.3.6b1 - Made replacing parameter on ╥copyFile╙ accept yes/no/ask & error when used with directories.
  397. 1.3.6b2 - Fixed ╥walk folders╙ bug when walking folders; files were fine.
  398. 1.3.6b3 - Fixed ╥finder selection╙ crash when trash is selected.  There are probably still problems with selecting PowerTalk items.
  399. 1.3.6b4 - Added ╥check bit╙ parameter to ╥machine environment╙ command. 
  400. 1.3.6 - official release
  401. 1.3.7b1 - Fixed some comments in the dictionary. Changed the ╥check bit╙ parameter of ╥machine environment╙ command to be zero based instead of one based, making it compatible with Inside Mac.  This affects scripts!
  402. 1.4b2 - Added ╥choose color╙ command.  Wondered if this change log was worth the bother. It╒s kind of embarassing, but I guess it╒s educational. I hope.  :)  Changed the version to 1.4 since we added a command.
  403. 1.4b3 - Added ╥owner name╙ and ╥sharing name╙ properties to ╥machine information╙ record.
  404. 1.4b4 - Fixed ╥CPU type╙ and ╥FPU╙ to work properly with PowerPC machines and added ╥PowerPC╙ property. Added description and examples for ╥screen list╙ command to this document. It was always missing and no one noticed.
  405. 1.4 - official release
  406.  
  407. Feel free to send money or to write with questions, comments or suggestions.
  408.  
  409. Jon Pugh
  410. One Infinite Loop MS 303-3A
  411. Cupertino, CA 95014
  412. (408) 974-6029
  413. jonpugh@netcom.com
  414. http://www.infoworkshop.com/~jonpugh/
  415.